/**
*
*/
package org.javacommerce.paypal.servlet;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.javacommerce.paypal.APIUtil;
import org.javacommerce.paypal.ws.API;
import org.javacommerce.paypal.ws.APICredential;
import org.javacommerce.paypal.ws.APIException;
import PayPalAPI.api.ebay.DoExpressCheckoutPaymentResponseType;
import PayPalAPI.api.ebay.GetExpressCheckoutDetailsResponseType;
import PayPalAPI.api.ebay.SetExpressCheckoutResponseType;
import eBLBaseComponents.apis.ebay.DoExpressCheckoutPaymentRequestDetailsType;
import eBLBaseComponents.apis.ebay.SetExpressCheckoutRequestDetailsType;
/**
* POST method is for SetExpressCheckout, GET method is for
* GetExpressCheckoutDetails, PUT method is for DoExpressCheckoutPaymnet.
*
* @author Michael Blanton (mike@mikeblanton.com)
* @web.servlet name="ExpressCheckout"
* @web.servlet-mapping url-pattern="/ExpressCheckout"
*/
public class ExpressCheckoutServlet extends BaseServlet {
/**
*
*/
private static final long serialVersionUID = -917341446997788855L;
private static final Log LOG = LogFactory
.getLog(ExpressCheckoutServlet.class);
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
/**
* POST method is for calling SetExpressCheckout API call.
*/
protected void doPost(HttpServletRequest _request,
HttpServletResponse _response) throws ServletException, IOException {
runSetExpressCheckout(_request, _response);
}
/**
* @param _request
* @param _response
* @throws ServletException
*/
protected final void runSetExpressCheckout(HttpServletRequest _request, HttpServletResponse _response) throws ServletException {
SetExpressCheckoutRequestDetailsType request = APIUtil
.buildSetExpressCheckoutDetailsRequest(_request);
APICredential credentials;
try {
credentials = getAPICredentials(_request);
} catch (APIException e) {
writeException("EC-1003", "Error executing SetExpressCheckout API call", null, e, _response);
return;
}
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing SetExpressCheckout API call");
}
SetExpressCheckoutResponseType response = API.setExpressCheckout(
request, credentials);
if (LOG.isDebugEnabled()) {
LOG.debug("SetExpressCheckout API call returned ["
+ response.getAck() + "], building XML.");
}
writeObject(response, _response);
if (LOG.isDebugEnabled()) {
LOG.debug("XML written to Output Stream");
}
} catch (RemoteException e) {
writeException("EC-1000",
"Error executing SetExpressCheckout API call", credentials,
e, _response);
}
}
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest _request,
HttpServletResponse _response) throws ServletException, IOException {
runGetExpressCheckoutDetails(_request, _response);
}
/**
* @param _request
* @param _response
* @throws ServletException
*/
protected final void runGetExpressCheckoutDetails(HttpServletRequest _request, HttpServletResponse _response) throws ServletException {
APICredential credentials;
try {
credentials = getAPICredentials(_request);
} catch (APIException e) {
writeException("EC-1003", "Error executing SetExpressCheckout API call", null, e, _response);
return;
}
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing GetExpressCheckoutDetails API call");
}
GetExpressCheckoutDetailsResponseType response = API
.getExpressCheckoutDetails(_request
.getParameter(APIUtil.PARAM_TOKEN),
credentials);
if (LOG.isDebugEnabled()) {
LOG.debug("GetExpressCheckoutDetails API call returned ["
+ response.getAck() + "], building XML.");
}
writeObject(response, _response);
if (LOG.isDebugEnabled()) {
LOG.debug("XML written to Output Stream");
}
} catch (RemoteException e) {
writeException("EC-2000",
"Error executing GetExpressCheckoutDetails API call",
credentials, e, _response);
}
}
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doPut(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void doPut(HttpServletRequest _request,
HttpServletResponse _response) throws ServletException, IOException {
runDoExpressCheckoutPayment(_request, _response);
}
/**
* @param _request
* @param _response
* @throws ServletException
*/
protected final void runDoExpressCheckoutPayment(HttpServletRequest _request, HttpServletResponse _response) throws ServletException {
DoExpressCheckoutPaymentRequestDetailsType request = APIUtil
.buildDoExpressCheckoutPaymentDetailsRequest(_request);
APICredential credentials;
try {
credentials = getAPICredentials(_request);
} catch (APIException e) {
writeException("EC-1003", "Error executing SetExpressCheckout API call", null, e, _response);
return;
}
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing DoExpressCheckoutPayment API call");
}
DoExpressCheckoutPaymentResponseType response = API
.doExpressCheckoutPayment(request, credentials);
if (LOG.isDebugEnabled()) {
LOG.debug("SetExpressCheckout API call returned ["
+ response.getAck() + "], building XML.");
}
writeObject(response, _response);
if (LOG.isDebugEnabled()) {
LOG.debug("XML written to Output Stream");
}
} catch (RemoteException e) {
writeException("EC-3000",
"Error executing DoExpressCheckoutPayment API call",
credentials, e, _response);
}
}
}